package VLSys;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;

import java.util.*;


import VLSObjects.*;

public class Interface extends JFrame
{
    ImageStore backdrop;
    JFrame frame = this;
  
    JLayeredPane layeredPane = new JLayeredPane();
    LoginModule login = new LoginModule();
    MainModule mains = new MainModule(frame);
    String blank = "";
    String string[] = new String[5];
    String desig;
    
    
    /*
    ImageIcon ask = new ImageIcon("images/prompt_ask.png");
    ImageIcon notice = new ImageIcon("images/prompt_notice.png");
     */
    
    ImageIcon notice_green = new ImageIcon("images/prompt_notice_green.png");
    ImageIcon ask_red = new ImageIcon("images/prompt_ask_red.png");
    ImageIcon notice_red = new ImageIcon("images/prompt_notice_red.png");
    
    ImageStore icon = new ImageStore("images/main_module/vls_icon.png");
    
    Calendar calendar = Calendar.getInstance();
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

                
 
    public Interface(){
        setTitle("Vehicle Log System");
        setSize(428,194);
        setResizable(false);
        setBackground(Color.BLACK);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setIconImage(icon.getImage());
        
    

        add(login);
        login.user.requestFocus();
        getRootPane().setDefaultButton(login.loginButton);
    
    
        login.loginButton.addActionListener(new  ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                    
                ///*
                    login.setVisible(false);
                    add(mains);
                    setSize(804,595);
                    setLocationRelativeTo(null);
                    mains.setVisible(true);
                    mains.mainScreen.setVisible(true);
                    //mains.transaction.setVisible(true);
                    login.clearlogin();
                    getRootPane().setDefaultButton(mains.leftNavig[0]);
                    
                    //mains.transactionNew.setVisible(true);
                    //mains.transaction.pindutan[0].setIcon(mains.transaction.transNew2);
                //*/
                
                
                /*
                String usernameField = login.user.getText();
                String passwordField = new String(login.password.getPassword());
                
                UserManager checkLogin = new UserManager();
                boolean loginResult = checkLogin.checkUser(usernameField, passwordField);
                
                if(loginResult == true)
                {   
                    
                         JOptionPane.showMessageDialog(frame,
                                     "Login Successful",
                                     "Vehicle Logging Service", 
                                     JOptionPane.INFORMATION_MESSAGE,
                                     notice_green);
                        
                    login.setVisible(false);
                    add(mains);
                    setSize(804,595);
                    setLocationRelativeTo(null);
                    hideModulePanels();
                    mains.setVisible(true);
                    mains.mainScreen.setVisible(true);
                    login.clearlogin();
                    getRootPane().setDefaultButton(mains.leftNavig[0]);
                    //mains.transactionNew.setVisible(true);
                    //mains.transaction.loginButton[0].setIcon(mains.transaction.transNew2);
                    
                    
                    desig = checkLogin.designation(usernameField);
                    if(desig.equals("Cashier"))
                    {   mains.manage.pindutan[0].setEnabled(false);
                        mains.manage.pindutan[1].setEnabled(false);
                        mains.manage.pindutan[2].setEnabled(false);
                        mains.manage.pindutan[3].setEnabled(false);
                    }
                 }
                        
                else
                {
                        JOptionPane.showMessageDialog(frame,
                                 "Invalid Account",
                                 "Vehicle Logging Service", 
                                 JOptionPane.INFORMATION_MESSAGE,
                                 notice_red);
                }
                */
            }
        }
        );

    login.exitButton.addActionListener(new  ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                System.exit(0);
            }
        }
        );
    
    //Navigation Panel
   
    mains.leftNavig[0].addActionListener(new  ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                hideModulePanels();
                hideModuleIcon();
                mains.transaction.setVisible(true);
                mains.manage.setVisible(false);
                mains.reports.setVisible(false);
                mains.transactionNew.setVisible(true);
                mains.transaction.pindutan[0].setIcon(mains.transaction.transNew2);
            }
        }
        );
    
    mains.leftNavig[1].addActionListener(new  ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {     
                hideModulePanels();
                hideModuleIcon();
                mains.manage.setVisible(true);
                mains.transaction.setVisible(false);
                mains.reports.setVisible(false);
                /*
                if(desig.equals("Cashier"))
                {   
                    mains.addVeh.setVisible(true);
                    mains.manage.pindutan[4].setIcon(mains.manage.vehAdd1);
                }
                else
                {   
                */    mains.addSer.setVisible(true);
                    mains.manage.pindutan[0].setIcon(mains.manage.serAdd1);
//                }
            }
        }
        );
    
    mains.leftNavig[2].addActionListener(new  ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                hideModulePanels();
                hideModuleIcon();
                mains.reports.setVisible(true);
                mains.transaction.setVisible(false);
                mains.manage.setVisible(false);
                mains.reportsTransactions.setVisible(true);
                mains.reports.pindutan[3].setIcon(mains.reports.trans2);
            }
        }
        );
    
    //Logout
    mains.leftNavig[3].addActionListener(new  ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                int n = JOptionPane.showConfirmDialog(
                            frame,
                            "Are You Sure?",
                            "Vehicle Logging Service",
                            JOptionPane.YES_NO_OPTION,
                            JOptionPane.QUESTION_MESSAGE,
                            ask_red);
                if(n==0)
                {
                    JOptionPane.showMessageDialog(frame,
                                     "You have been Logged Out",
                                     "Vehicle Logging Service", 
                                     JOptionPane.INFORMATION_MESSAGE,
                                     notice_green);    
                    
                    mains.setVisible(false);     
                    login.setVisible(true);
                    setSize(428,194);
                    setLocationRelativeTo(null);
                    login.user.requestFocus();
                    getRootPane().setDefaultButton(login.loginButton);
                }
            }
        }
        );
    
    //Transaction Module
    
    mains.transaction.pindutan[0].addActionListener(new  ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                hideModulePanels();
                hideTransactionIcon();
                mains.transactionNew.setVisible(true);
                mains.transaction.pindutan[0].setIcon(mains.transaction.transNew2);
                universalReset();
            }
        }
        );

     mains.transaction.pindutan[1].addActionListener(new  ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                hideModulePanels();
                hideTransactionIcon();
                mains.transactionEdit.setVisible(true);
                mains.transaction.pindutan[1].setIcon(mains.transaction.transEdit2);
                universalReset();
                mains.transactionEdit.servicesModel.getDataVector().removeAllElements();
                mains.transactionEdit.servicesModel.fireTableDataChanged();
                mains.transactionEdit.populateTable("SELECT transaction.transaction_id, transaction_date, transaction_time, plate_num, SUM(servicetransaction.amount) AS total_amount  FROM `transaction` JOIN servicetransaction ON transaction.transaction_id = servicetransaction.transaction_id WHERE transaction_date = '" + dateFormat.format(calendar.getTime()) + "'  GROUP BY transaction.transaction_id ORDER BY transaction.transaction_id ASC");
            }
        }
        );
     
     mains.transaction.pindutan[2].addActionListener(new  ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                hideModulePanels();
                hideTransactionIcon();
                mains.transactionExpense.setVisible(true);
                mains.transaction.pindutan[2].setIcon(mains.transaction.addExpense2);
                universalReset();
            }
        }
        );
   
    //Manager Module
    
    mains.manage.pindutan[0].addActionListener(new  ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {   
                hideModulePanels();
                hideManageIcon();
                mains.addSer.setVisible(true);
                mains.manage.pindutan[0].setIcon(mains.manage.serAdd1);
                universalReset();
            }
        }
        );
    
    mains.manage.pindutan[1].addActionListener(new  ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {   
                hideModulePanels();
                hideManageIcon();
                mains.editSer.setVisible(true);
                mains.manage.pindutan[1].setIcon(mains.manage.serEdit1);
                universalReset();
                mains.editSer.populateTable("SELECT * FROM servicelist");
            }
        }
        );
    
    mains.manage.pindutan[2].addActionListener(new  ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {   
                hideModulePanels();
                hideManageIcon();
                mains.addNew.setVisible(true);
                mains.manage.pindutan[2].setIcon(mains.manage.userAdd1);
                universalReset();
            }
        }
        );
    
    mains.manage.pindutan[3].addActionListener(new  ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {   
                hideModulePanels();
                hideManageIcon();
                mains.editNew.setVisible(true);
                mains.manage.pindutan[3].setIcon(mains.manage.userEdit1);
                universalReset();
                mains.editNew.populateTable("SELECT * FROM userlist ORDER BY userID");
            }
        }
        );
    
    mains.manage.pindutan[4].addActionListener(new  ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {   
                hideModulePanels();
                hideManageIcon();
                mains.addVeh.setVisible(true);
                mains.manage.pindutan[4].setIcon(mains.manage.vehAdd1);
                universalReset();
            }
        }
        );
    
     mains.manage.pindutan[5].addActionListener(new  ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {     
                hideModulePanels();
                hideManageIcon();
                mains.editVeh.setVisible(true);
                mains.manage.pindutan[5].setIcon(mains.manage.vehEdit1);
                universalReset();
                mains.editVeh.populateTable("SELECT * FROM vehiclelist ORDER BY vehicleID");
            }
        }
        );     
     
    //Reports Module
    
    mains.reports.pindutan[0].addActionListener(new  ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                hideModulePanels();
                hideReportIcon();
                mains.reportsExpenses.setVisible(true);
                mains.reports.pindutan[0].setIcon(mains.reports.exp2);
                //mains.reportsExpenses.populateTable("SELECT * FROM expense");
                mains.reportsExpenses.getOldestLatestEntry();
            }
        }
        );
    
    mains.reports.pindutan[1].addActionListener(new  ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                hideModulePanels();
                hideReportIcon();
                mains.reportsFinancial.setVisible(true);
                mains.reportsFinancial.getOldestLatestTransaction();
                mains.reports.pindutan[1].setIcon(mains.reports.finRep2);  
               
            }
        }
        );
    
    mains.reports.pindutan[2].addActionListener(new  ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                hideModulePanels();
                hideReportIcon();
                mains.reportsServices.setVisible(true);
                mains.reports.pindutan[2].setIcon(mains.reports.serv2);
                mains.reportsServices.populateTable("SELECT * FROM servicelist");
                
            }
        }
        );
    
    mains.reports.pindutan[3].addActionListener(new  ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                hideModulePanels();
                hideReportIcon();
                mains.reportsTransactions.setVisible(true);
                mains.reports.pindutan[3].setIcon(mains.reports.trans2);
                mains.reportsTransactions.populateTable();
                mains.reportsTransactions.filter.setText("");
                mains.reportsTransactions.getOldestLatestTransaction();
            }
        }
        );
    
    mains.reports.pindutan[4].addActionListener(new  ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                hideModulePanels();
                hideReportIcon();
                mains.reportsUsers.setVisible(true);
                mains.reports.pindutan[4].setIcon(mains.reports.user2);
                mains.reportsUsers.populateTable("SELECT * FROM userlist");
                mains.reportsServices.serviceTable.setAutoCreateRowSorter(false);
            }
        }
        );
    
    mains.reports.pindutan[5].addActionListener(new  ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                hideModulePanels();
                hideReportIcon();
                mains.reportsVehicles.setVisible(true);
                mains.reportsVehicles.populateTable("SELECT * FROM vehiclelist");
            }
        }
        );
    
    }    
    //Hide Panels
    
    public void hideTransactionPanels()
    {   
           mains.transactionNew.setVisible(false);
           mains.transactionEdit.setVisible(false);
           mains.transactionExpense.setVisible(false);
           mains.mainScreen.setVisible(false);
    }
    
    public void hideManagementPanels()
    {
           mains.editVeh.setVisible(false);
           mains.addVeh.setVisible(false);
           mains.editNew.setVisible(false);
           mains.addNew.setVisible(false);
           mains.addSer.setVisible(false);
           mains.editSer.setVisible(false);
           mains.mainScreen.setVisible(false);
    }
    
    public void hideReportsPanels()
    {
            mains.reportsExpenses.setVisible(false);
            mains.reportsFinancial.setVisible(false);
            mains.reportsServices.setVisible(false);
            mains.reportsTransactions.setVisible(false);
            mains.reportsUsers.setVisible(false);
            mains.reportsVehicles.setVisible(false);
            mains.mainScreen.setVisible(false);
    }
    
    public void hideModulePanels()
    {      hideTransactionPanels();
           hideManagementPanels();
           hideReportsPanels();
           
    }
    
    //Hide Icon
    
    public void hideTransactionIcon()
    {
            mains.transaction.pindutan[0].setIcon(mains.transaction.transNew);
            mains.transaction.pindutan[1].setIcon(mains.transaction.transEdit);
            mains.transaction.pindutan[2].setIcon(mains.transaction.addExpense);
    }
    
    
    
    public void hideManageIcon()
    {
        mains.manage.pindutan[0].setIcon(mains.manage.serAdd);
        mains.manage.pindutan[1].setIcon(mains.manage.serEdit);
        mains.manage.pindutan[2].setIcon(mains.manage.userAdd);
        mains.manage.pindutan[3].setIcon(mains.manage.userEdit);
        mains.manage.pindutan[4].setIcon(mains.manage.vehAdd);
        mains.manage.pindutan[5].setIcon(mains.manage.vehEdit);
    }
    
    public void hideReportIcon()
    {
        mains.reports.pindutan[0].setIcon(mains.reports.exp1);
        mains.reports.pindutan[1].setIcon(mains.reports.finRep1);
        mains.reports.pindutan[2].setIcon(mains.reports.serv1);
        mains.reports.pindutan[3].setIcon(mains.reports.trans1);
        mains.reports.pindutan[4].setIcon(mains.reports.user1);
        mains.reports.pindutan[5].setIcon(mains.reports.veh1);
        
    }
    
    public void hideModuleIcon()
    {
        hideTransactionIcon();
        hideReportIcon();
        hideManageIcon();
    }
    

    public void universalReset()
    {
        mains.addNew.resetValues();
        mains.editNew.resetValues();
        mains.addSer.resetValues();
        mains.editSer.resetValues();
        mains.addVeh.resetValues();
        mains.editVeh.resetValues();
        mains.transactionNew.resetValues();
        mains.transactionEdit.resetValues();
        mains.transactionExpense.resetValues();
    }
    

}



    
